<template>
  <el-form :model="item" ref="moForm" :rules="rules" label-width="130px">
    <el-form-item prop="name" label="名称">
      <el-input v-model.trim="item.name" placeholder="请输入名称"></el-input>
    </el-form-item>
    <el-form-item prop="imageFile" label="图片">
      <el-button class="setimg" type="primary" @click="setImg"
        >选择图片</el-button
      >
      <br />
      <span class="imginfo">请选择.jpg/.png格式图片，大小不超过1M</span>
      <input
        type="file"
        accept=".jpg, .png"
        id="fileInput"
        @change="imgChange($event)"
      />
      <div class="imgbox">
        <img :src="iconImage" id="image" />
      </div>
    </el-form-item>
    <el-form-item prop="jifen" label="积分">
      <el-input v-model.trim="item.jifen" placeholder="请输入积分"></el-input>
    </el-form-item>
    <el-form-item prop="probability" label="中奖比例">
      <el-input
        v-model.trim="item.probability"
        placeholder="请输入中奖比例"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSubmitClick()">提交</el-button>
      <el-button @click="handleBackClick">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: ["item"],
  data() {
    return {
      reader: null,
      imgBase: "",
      iconImage: "",
      cates: [],
      rules: {
        name: [{ required: true, trigger: "blur", message: "名称" }],
        jifen: [{ required: true, trigger: "blur", message: "积分" }],
        probability: [{ required: true, message: "中奖比例", trigger: "blur" }],
        imageFile: [{ required: true, message: "请选择图片", trigger: "blur" }],
        // link: [
        //   { required: true, message: "请选择活动链接", trigger: "blur" },
        //   { type: "url", message: "请输入正确格式的链接", trigger: "blur" },
        // ],
        // endTime: [
        //   {
        //     validator: check_date,
        //     message: "请选择合法的开始结束时间",
        //     trigger: "blur",
        //   },
        // ],
      },
    };
  },
  computed: {
    obj() {
      return {
        name: this.item.name,
        jifen: this.item.jifen,
        probability: this.item.probability,
        image: this.item.image,
        imageFile: this.item.imageFile,
      };
    },
  },
  watch: {},
  created() {
    this.reader = new FileReader();
    this.reader.onload = (e) => {
      this.imgBase = e.target.result;
    };
    setTimeout(() => {
      if (this.item.image) {
        this.iconImage = this.item.image;
      }
    }, 500);
  },
  methods: {
    handleBackClick() {
      this.$router.go(-1);
    },
    handleSubmitClick() {
      this.$emit("doSubmit");
    },
    valid(callback) {
      this.$refs["moForm"].validate((valid) => {
        callback(valid);
      });
    },
    setImg() {
      const target = document.getElementById("fileInput");
      target && target.click();
    },
    imgChange(e) {
      console.log(e);
      const img = e.target.files[0];
      if (img.size / 1024 < 2 * 1024) {
        this.reader.readAsDataURL(img);
        this.item.imageFile = img;
        setTimeout(() => {
          const pos = this.imgBase.indexOf("base64") + 7;
          this.imgBase = this.imgBase.substr(pos);
          this.iconImage = "data:image/png;base64," + this.imgBase;
        }, 500);
      } else {
        this.$message.error("图片大小不可超过2M");
      }
      this.$refs["moForm"].validateField("imageFile");
    },
  },
};
</script>

<style scoped>
#fileInput {
  width: 0;
  height: 0;
  overflow: hidden;
}

.setimg {
  float: left;
  margin-left: 0;
}

.imgbox {
  border: 1px solid #eee;
  width: 200px;
  height: auto;
}

.imgbox img {
  align: center;
  max-width: 100%;
}

.imginfo {
  font-size: 12px;
  color: #666;
}
</style>
